/*====================================================================================
    EVS Codec 3GPP TS26.443 Nov 13, 2018. Version 12.11.0 / 13.7.0 / 14.3.0 / 15.1.0
  ====================================================================================*/

#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <sys/stat.h>
#include "options.h"
#include "cnst.h"
#include "prot.h"
#include "rom_com.h"
#include "g192.h"

/*------------------------------------------------------------------------------------------*
 * Global variables
 *------------------------------------------------------------------------------------------*/
long enc_frame = 0;                 /* Counter of frames */



/*------------------------------------------------------------------------------------------*
 * Main encoder function
 *------------------------------------------------------------------------------------------*/

void LibEVS_init_encoder(void *st)
{
    Encoder_State *pst = (Encoder_State*)st;
    init_encoder(pst);
}

void LibEVS_destroy_encoder(void *st)
{
    Encoder_State *pst = (Encoder_State*)st;
    destroy_encoder(pst);
}

void LibEVS_enc_free_params(void **st)
{
    Encoder_State *pst = (Encoder_State*)(*st);
    free(pst);
    *st = NULL;
}

void LibEVS_evs_enc(void *st,const short *data,const short n_samples)
{
    Encoder_State *pst = (Encoder_State*)st;
    evs_enc(pst, data, n_samples);
}

void LibEVS_amr_wb_enc(void *st,const short *data,const short n_samples)
{
    Encoder_State *pst = (Encoder_State*)st;
    amr_wb_enc(pst, data, n_samples);
}

void LibEVS_reset_rf_indices(void *st)
{
    Encoder_State *pst = (Encoder_State*)st;
    reset_rf_indices(pst);
}

void LibEVS_reset_indices_enc(void *st)
{
    Encoder_State *pst = (Encoder_State*)st;
    reset_indices_enc(pst);
}

void LibEVS_indices_to_serial(const void *st,UWord8 *pFrame,Word16 *pFrame_size)
{
    Encoder_State *pst = (Encoder_State*)st;
    indices_to_serial(pst, pFrame, pFrame_size);
}

int LibEVS_enc_init_params(int sampling,int bitRate,void **st)
{
    if ((*st = (Encoder_State *)malloc(sizeof(Encoder_State))) == NULL)
    {
        fprintf(stderr, "Can not allocate memory for encoder state structure\n");
        return (-1);
    }

    //Encoder_State *pst = (Encoder_State*)st;
    enc_init_params(sampling, bitRate, *st);

    return 0;
}

int LibEVS_set_Indice(void *st,Indice *indice)
{
    Encoder_State *pst = (Encoder_State*)st;
    pst->ind_list = indice;

    return 0;
}

void LibEVS_set_8K_NB_maxbitrate(void *pst)
{
    Encoder_State *st = (Encoder_State*)pst;
    if (((st->input_Fs == 8000) || (st->max_bwidth == NB)) && (st->total_brate > ACELP_24k40))
    {
        st->total_brate = ACELP_24k40;
        st->codec_mode = MODE2;
    }
}

short LibEVS_get_input_Size(void *pst)
{
    Encoder_State *st = (Encoder_State*)pst;
    return (short)(st->input_Fs / 50);

}

void LibEVS_write_indices_2_buf(void *st,Word16 *outSize,UWord8 *pFrame,Word16 pFrame_size)
{
    Encoder_State *pst = (Encoder_State*)st;
    write_indices_2_buf(pst, outSize, pFrame, pFrame_size);
}
